home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PASCSRC.ZIP / VARREC.PAS < prev    next >
Pascal/Delphi Source File  |  1988-01-15  |  2KB  |  60 lines

  1.                                 (* Chapter 9 - Program 3 *)
  2. program Variant_Record_Example;
  3.  
  4. type Kind_Of_Vehicle = (Car,Truck,Bicycle,Boat);
  5.  
  6.      Vehicle = record
  7.        Owner_Name   : string[25];
  8.        Gross_Weight : integer;
  9.        Value        : real;
  10.        case What_Kind : Kind_Of_Vehicle of
  11.          Car     : (Wheels : integer;
  12.                     Engine : string[8]);
  13.          Truck   : (Motor  : string[8];
  14.                     Tires  : integer;
  15.                     Payload : integer);
  16.          Bicycle : (Tyres   : integer);
  17.          Boat    : (Prop_Blades : byte;
  18.                     Sail    : boolean;
  19.                     Power   : string[8]);
  20.        end; (* of record *)
  21.  
  22. var Sunfish,Ford,Schwinn,Mac : Vehicle;
  23.  
  24. begin  (* main program *)
  25.    Ford.Owner_Name := 'Walter'; (* fields defined in order *)
  26.    Ford.Gross_Weight := 5750;
  27.    Ford.Value := 2595.00;
  28.    Ford.What_Kind := Truck;
  29.    Ford.Motor := 'V8';
  30.    Ford.Tires := 18;
  31.    Ford.Payload := 12000;
  32.  
  33.    with Sunfish do begin
  34.       What_Kind := Boat; (* fields defined in random order *)
  35.       Sail := TRUE;
  36.       Prop_Blades := 3;
  37.       Power := 'wind';
  38.       Gross_Weight := 375;
  39.       Value := 1300.00;
  40.       Owner_Name := 'Herman and George';
  41.    end;
  42.  
  43.    Ford.Engine := 'flathead';  (* tag-field not defined yet but it *)
  44.    Ford.What_Kind := Car;      (* must be before it can be used    *)
  45.    Ford.Wheels := 4;
  46.       (* notice that the non variant part is not redefined here *)
  47.  
  48.    Mac := Sunfish; (* entire record copied, including the tag-field *)
  49.  
  50.    if Ford.What_Kind = Car then        (* this should print *)
  51.       Writeln(Ford.Owner_Name,' owns the car with a ',Ford.Engine,
  52.               ' engine');
  53.  
  54.    if Sunfish.What_Kind = Bicycle then  (* this should not print *)
  55.       Writeln('The sunfish is a bicycle which it shouldn''t be');
  56.  
  57.    if Mac.What_Kind = Boat then         (* this should print *)
  58.       Writeln('The mac is now a boat with',Mac.Prop_Blades:2,
  59.                ' propeller blades.');
  60. end.  (* of main program *)